import {set, del} from "../observer/index";
import {
    extend,
    mergeOptions,
    defineReactive
} from '../util/index';
import {ASSET_TYPES} from "../../shared/constants";
import {initMixin} from "./mixin";
import {initExtend} from "./extend";
import {initAssetsRegister} from "./assets";
import {createPatchFunction, PatchOption} from "../vdom";
import {nodeOperations} from "../../platform/web/patch/node-operation";
import {modules} from "../../platform/web/module";
import {nextTick} from "../util/next-tick";
import {observe} from "../observer/observer";

export function initGlobalAPI (Reactive) {

    // 工具集
    Reactive.util = {
        // todo warn 函数
        // 此 extend 与 initExtend 的区别, 此 extend 是将源对象上的键赋值到目的对象上
        // initExtend 添加的方法是类似于类继承方式的添加方法
        extend,
        mergeOptions,
        defineReactive
    };

    Reactive.prototype.$nextTick = nextTick;
    Reactive.prototype.$observe = observe;

    // 创建 options 配置
    Reactive.options = Object.create(null);
    ASSET_TYPES.forEach(type => {
        Reactive.options[type + 's'] = Object.create(null);
    });
    // 设置 _base 属性
    Reactive.options._base = Reactive;

    // 为 Reactive 添加 mixin (混入)函数
    initMixin(Reactive);
    // 为 Reactive 添加 extend (类继承)函数
    initExtend(Reactive);
    // todo 为 Reactive 添加 use（插件） 函数

    // 为 Reactive 添加资源的处理函数
    initAssetsRegister(Reactive);

    // 为 Reactive 实例添加 patch 函数
    const patchOptions = new PatchOption(
        modules,
        nodeOperations
    );
    Reactive.prototype.__patch__ = createPatchFunction(patchOptions);
}

